import { getReadTime } from './localStorage'
import { realPx } from './utils'

// 字体大小
export const FONT_SIZE_LIST = [
  { fontSize: 12 },
  { fontSize: 14 },
  { fontSize: 16 },
  { fontSize: 18 },
  { fontSize: 20 },
  { fontSize: 22 },
  { fontSize: 24 }
]
// 字体名称
export const FONT_FAMILY = [
  { font: 'Default' },
  { font: 'Cabin' },
  { font: 'Days One' },
  { font: 'Montserrat' },
  { font: 'Tangerine' }
]
/* eslint-disable */ // 在文件开头禁用 eslint 检查
// 主题列表
export const themeList = [
  {
    alias: 'book.themeDefault',
    name: 'Default',
    style: {
      body: {
        color: '#4c5059',
        background: '#cecece',
        'padding-top': `${realPx(48)}px!important`,
        'padding-bottom': `${realPx(48)}px!important`
      }
    }
  },
  {
    alias: 'book.themeGold',
    name: 'Gold',
    style: {
      body: {
        'color': '#5c5b56',
        'background': '#c6c2b6',
        'padding-top': `${realPx(48)}px!important`,
        'padding-bottom': `${realPx(48)}px!important`
      }
    }
  },
  {
    alias: 'book.themeEye',
    name: 'Eye',
    style: {
      body: {
        'color': '#404c42',
        'background': '#a9c1a9',
        'padding-top': `${realPx(48)}px!important`,
        'padding-bottom': `${realPx(48)}px!important`
      }
    }
  },
  {
    alias: 'book.themeNight',
    name: 'Night',
    style: {
      body: {
        'color': '#cecece',
        'background': '#000000',
        'padding-top': `${realPx(48)}px!important`,
        'padding-bottom': `${realPx(48)}px!important`
      }
    }
  }
]
// 全局主题配置
export const initGlobalStyle = (defaultTheme) => {
  removeAllCss()
  switch(defaultTheme) {
    case 'Default':
      addCss(`${process.env.VUE_APP_RES_URL}/theme/theme_default.css`)
      break
    case 'Eye':
      addCss(`${process.env.VUE_APP_RES_URL}/theme/theme_eye.css`)
      break
    case 'Gold':
      addCss(`${process.env.VUE_APP_RES_URL}/theme/theme_gold.css`)
      break
    case 'Night':
      addCss(`${process.env.VUE_APP_RES_URL}/theme/theme_night.css`)
      break
    default:
      addCss(`${process.env.VUE_APP_RES_URL}/theme/theme_default.css`)
      break
  }
}
export const addCss = (href) => {
  const link = document.createElement('link')
  link.setAttribute('rel', 'stylesheet')
  link.setAttribute('type', 'text/css')
  link.setAttribute('href', href)
  document.getElementsByTagName('head')[0].appendChild(link)
}

export const removeCss = (href) => {
  const links = document.getElementsByTagName('link')
  for (let i = links.length; i >= 0; i--) {
    const link = links[i]
    if (link && link.getAttribute('href') && link.getAttribute('href') === href) {
      link.parentNode.removeChild(link)
    }
  }
}

export const removeAllCss = () => {
  removeCss(`${process.env.VUE_APP_RES_URL}/theme/theme_default.css`)
  removeCss(`${process.env.VUE_APP_RES_URL}/theme/theme_eye.css`)
  removeCss(`${process.env.VUE_APP_RES_URL}/theme/theme_gold.css`)
  removeCss(`${process.env.VUE_APP_RES_URL}/theme/theme_night.css`)
}

export const getReadTimeByMinute = (fileName) => {
  const readTime = getReadTime(fileName)
  if (!readTime) {
    return 0
  } else {
    return Math.ceil(readTime / 60)
  }
}

// 递归展开多维数组
export const flatten = (array) => {
  return [].concat(...array.map(item => [].concat(item, ...flatten(item.subitems))))
}